{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 泰坦尼克号生存率预测"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "目录\r\n",
    "1. 提出问题（Business Understanding ）\r\n",
    "2. 理解数据（Data Understanding）\r\n",
    " * 采集数据\r\n",
    " * 导入数据\r\n",
    " * 查看数据集信息\r\n",
    "3. 数据清洗（Data Preparation ）\r\n",
    " * 数据预处理  \r\n",
    "&nbsp;- 选择子集  \r\n",
    "&nbsp;- 列名重命名  \r\n",
    "&nbsp;- 缺失数据处理  \r\n",
    "&nbsp;- 数据类型转换  \r\n",
    "&nbsp;- 数据排序  \r\n",
    "&nbsp;- 异常值处理\r\n",
    " * 特征工程（Feature Engineering）  \r\n",
    "&nbsp;- 特征提取   \r\n",
    "&emsp;&emsp;- 数值类型  \r\n",
    "&emsp;&emsp;- 时间序列  \r\n",
    "&emsp;&emsp;- 分类数据  \r\n",
    "&nbsp;- 特征选择\r\n",
    "4. 构建模型（Modeling） \r\n",
    "* 建立训练数据集和测试数据集\r\n",
    "* 选择机器学习算法  \r\n",
    "&nbsp;- 导入算法  \r\n",
    "&nbsp;- 创建模型  \r\n",
    "* 训练模型\r\n",
    "5. 模型评估（Evaluation） \r\n",
    "6. 方案实施 （Deployment）\r\n",
    " * 提交结果到Kaggle\r\n",
    " * 报告撰写"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 1.提出问题\r\n",
    "什么样的人在泰坦尼克号中更容易存活？"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2.理解数据"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.1 采集数据\r\n",
    "从Kaggle泰坦尼克号项目页面下载数据：https://www.kaggle.com/c/titanic"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2 导入数据"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# 忽略警告提示\r\n",
    "import warnings\r\n",
    "warnings.filterwarnings('ignore')\r\n",
    "\r\n",
    "# 导入处理数据包\r\n",
    "import numpy as np\r\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "# 导入数据\r\n",
    "# 导入训练数据集\r\n",
    "train = pd.read_csv('./train.csv') # ./代表目前程序所在的目录\r\n",
    "# 导入测试数据集\r\n",
    "test = pd.read_csv('./test.csv')\r\n",
    "# 查看数据集的形状\r\n",
    "print('训练数据集：', train.shape, '\\n测试数据集：', test.shape) \r\n",
    "# 这里要记住训练数据集有891条数据，方便后面从中拆分出测试数据集用于提交Kaggle结果"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "训练数据集： (891, 12) \n",
      "测试数据集： (418, 11)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **Note:** 训练数据中有12个变量，测试数据中有11个变量。这是因为训练数据中多了个生存率情况预测。因为这是要预测的值，所以测试数据中是没有的。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "# 查看训练数据集和测试数据集中的行数\r\n",
    "rowNum_train = train.shape[0]\r\n",
    "rowNum_test = test.shape[0]\r\n",
    "\r\n",
    "print('kaggle训练数据集有多少行数据：', rowNum_train,\r\n",
    "        '\\nkaggle测试数据集有多少行数据：', rowNum_test)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "kaggle训练数据集有多少行数据： 891 \n",
      "kaggle测试数据集有多少行数据： 418\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# 合并数据集，方便同时对两个数据集进行清洗\r\n",
    "full = train.append(test, ignore_index = True)\r\n",
    "\r\n",
    "print('合并后的数据集：', full.shape)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "合并后的数据集： (1309, 12)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3 查看数据集信息"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Note: \r\n",
    "Passengerld 乘客编号  \r\n",
    "Survived 生存情况  \r\n",
    "Pclass 客舱等级  \r\n",
    "Name 姓名  \r\n",
    "Sex 性别  \r\n",
    "Age 年龄  \r\n",
    "SibSp 同代直系亲属数  \r\n",
    "Parch 不同代直系亲属数  \r\n",
    "Ticket 船票编号  \r\n",
    "Fare 船票价格  \r\n",
    "Cabin 客舱号  \r\n",
    "Embarked 登船港口"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "# 获取数据类型列的描述统计信息\r\n",
    "full.describe()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1309.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>1309.000000</td>\n",
       "      <td>1046.000000</td>\n",
       "      <td>1309.000000</td>\n",
       "      <td>1309.000000</td>\n",
       "      <td>1308.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>655.000000</td>\n",
       "      <td>0.383838</td>\n",
       "      <td>2.294882</td>\n",
       "      <td>29.881138</td>\n",
       "      <td>0.498854</td>\n",
       "      <td>0.385027</td>\n",
       "      <td>33.295479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>378.020061</td>\n",
       "      <td>0.486592</td>\n",
       "      <td>0.837836</td>\n",
       "      <td>14.413493</td>\n",
       "      <td>1.041658</td>\n",
       "      <td>0.865560</td>\n",
       "      <td>51.758668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.170000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>328.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>21.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.895800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>655.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.454200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>982.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.275000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1309.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived       Pclass          Age        SibSp  \\\n",
       "count  1309.000000  891.000000  1309.000000  1046.000000  1309.000000   \n",
       "mean    655.000000    0.383838     2.294882    29.881138     0.498854   \n",
       "std     378.020061    0.486592     0.837836    14.413493     1.041658   \n",
       "min       1.000000    0.000000     1.000000     0.170000     0.000000   \n",
       "25%     328.000000    0.000000     2.000000    21.000000     0.000000   \n",
       "50%     655.000000    0.000000     3.000000    28.000000     0.000000   \n",
       "75%     982.000000    1.000000     3.000000    39.000000     1.000000   \n",
       "max    1309.000000    1.000000     3.000000    80.000000     8.000000   \n",
       "\n",
       "             Parch         Fare  \n",
       "count  1309.000000  1308.000000  \n",
       "mean      0.385027    33.295479  \n",
       "std       0.865560    51.758668  \n",
       "min       0.000000     0.000000  \n",
       "25%       0.000000     7.895800  \n",
       "50%       0.000000    14.454200  \n",
       "75%       0.000000    31.275000  \n",
       "max       9.000000   512.329200  "
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Note:\r\n",
    "count 数量  \r\n",
    "mean 均值  \r\n",
    "std 标准差\r\n",
    "min 最小值\r\n",
    "25% 下四分位数  \r\n",
    "50% 中位数  \r\n",
    "75% 上四分位数\r\n",
    "max 最大值"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "# 查看每一列的数据类型和数据总数\r\n",
    "full.info()\r\n",
    "\r\n",
    "'''\r\n",
    "数据总数为1309行\r\n",
    "\r\n",
    "数据缺失情况：\r\n",
    "1. 数据类型列。包括 年龄Age(float64)，船票价格Fare(float64)\r\n",
    "    1) 年龄Age数据总数为1046条，缺失了1309-1046=263，缺失率(263/1309)*100=20%，缺失较多\r\n",
    "    2) 船票价格Fare数据总数为1308，缺失了1309-1308=1，缺失较少\r\n",
    "\r\n",
    "2. 字符串列。包括 客舱号Cabin(object)，登船港口Embarked(object)\r\n",
    "    1) 客舱号Cabin数据总数为295，缺失了1309-295=1014，缺失率(1014/1309)*100=77.5%，缺失较多\r\n",
    "    2) 登船港口Embarked数据总数为1307，缺失了1309-1307=2，缺失较少\r\n",
    "\r\n",
    "这为我们下一步数据清洗指明了方向，只有知道哪些数据缺失数据，我们才能有针对性的处理。\r\n",
    "'''"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1046 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1308 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1307 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'\\n数据总数为1309行\\n\\n数据缺失情况：\\n1. 数据类型列。包括 年龄Age(float64)，船票价格Fare(float64)\\n    1) 年龄Age数据总数为1046条，缺失了1309-1046=263，缺失率(263/1309)*100=20%，缺失较多\\n    2) 船票价格Fare数据总数为1308，缺失了1309-1308=1，缺失较少\\n\\n2. 字符串列。包括 客舱号Cabin(object)，登船港口Embarked(object)\\n    1) 客舱号Cabin数据总数为295，缺失了1309-295=1014，缺失率(1014/1309)*100=77.5%，缺失较多\\n    2) 登船港口Embarked数据总数为1307，缺失了1309-1307=2，缺失较少\\n\\n这为我们下一步数据清洗指明了方向，只有知道哪些数据缺失数据，我们才能有针对性的处理。\\n'"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3.数据清洗（Data Preparation ）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.1 数据预处理\r\n",
    "1.  选择子集\r\n",
    "2.\t列名重命名\r\n",
    "3.\t缺失数据处理\r\n",
    "4.\t数据类型转换\r\n",
    "5.\t数据排序\r\n",
    "6.\t异常值处理"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  缺失数据处理\r\n",
    "很多机器学习算法为了训练模型，要求所传入的特征中不能有空值。\r\n",
    "\r\n",
    "\r\n",
    "1. 如果是数值类型，用平均值取代\r\n",
    "2. 如果是分类数据，用最常见的类别取代\r\n",
    "3. 使用模型预测缺失值，例如：K-NN"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "'''\r\n",
    "数据缺失情况：\r\n",
    "1. 数据类型列。包括 年龄Age(float64)，船票价格Fare(float64)\r\n",
    "    1) 年龄Age数据总数为1046条，缺失了1309-1046=263，缺失率(263/1309)*100=20%，缺失较多\r\n",
    "    2) 船票价格Fare数据总数为1308，缺失了1309-1308=1，缺失较少\r\n",
    "\r\n",
    "对于数据类型，处理缺失值最简单的方法就是用平均数来填充缺失值\r\n",
    "'''\r\n",
    "\r\n",
    "print('处理前：')\r\n",
    "full.info()\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理前：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1046 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1308 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1307 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "# 年龄Age\r\n",
    "full['Age'] = full['Age'].fillna(full['Age'].mean())\r\n",
    "# 船票价格Fare\r\n",
    "full['Fare'] = full['Fare'].fillna(full['Fare'].mean())\r\n",
    "print('处理后：')\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理后：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1307 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "# 检查数据处理是否正常\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "'''\r\n",
    "数据缺失情况：\r\n",
    "2. 字符串列。包括 客舱号Cabin(object)，登船港口Embarked(object)\r\n",
    "    1) 客舱号Cabin数据总数为295，缺失了1309-295=1014，缺失率(1014/1309)*100=77.5%，缺失较多\r\n",
    "    2) 登船港口Embarked数据总数为1307，缺失了1309-1307=2，缺失较少\r\n",
    "'''\r\n",
    "# 登船港口（Embarked）：查看里面数据长啥样\r\n",
    "full['Embarked'].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    S\n",
       "1    C\n",
       "2    S\n",
       "3    S\n",
       "4    S\n",
       "Name: Embarked, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Note:\r\n",
    "出发地点：S=英国南安普顿Southampton  \r\n",
    "途径地点1：C=法国 瑟堡市Cherbourg  \r\n",
    "途径地点2：Q=爱尔兰 昆士敦Queenstown"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "'''\r\n",
    "分类变量Embarked，看下最常见的类别，用其填充\r\n",
    "'''\r\n",
    "full['Embarked'].value_counts()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "S    914\n",
       "C    270\n",
       "Q    123\n",
       "Name: Embarked, dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "'''\r\n",
    "从结果来看，S类别最常见。我们将缺失值填充为最频繁出现的值：\r\n",
    "S=英国南安普顿Southampton\r\n",
    "'''\r\n",
    "\r\n",
    "print('处理前：')\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理前：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1307 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "# 登船港口Embarked\r\n",
    "print('处理后：')\r\n",
    "full['Embarked'] = full['Embarked'].fillna('S')\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理后：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1309 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "# 检查数据处理是否正常\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "'''\r\n",
    "数据缺失情况：\r\n",
    "2. 字符串列。包括 客舱号Cabin(object)，登船港口Embarked(object)\r\n",
    "    1) 客舱号Cabin数据总数为295，缺失了1309-295=1014，缺失率(1014/1309)*100=77.5%，缺失较多\r\n",
    "    2) 登船港口Embarked数据总数为1307，缺失了1309-1307=2，缺失较少\r\n",
    "'''\r\n",
    "# 客舱号（Cabin）: 查看里面数据长啥样\r\n",
    "full['Cabin'].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0     NaN\n",
       "1     C85\n",
       "2     NaN\n",
       "3    C123\n",
       "4     NaN\n",
       "Name: Cabin, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "# 缺失数据比较多，客舱号（Cabin）缺失值填充为U，表示未知（Unknown）\r\n",
    "print('处理前：')\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理前：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        295 non-null    object \n",
      " 11  Embarked     1309 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "# 客舱号Cabin\r\n",
    "print('处理后：')\r\n",
    "full['Cabin'] = full['Cabin'].fillna('U')\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "处理后：\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        1309 non-null   object \n",
      " 11  Embarked     1309 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "# 检查数据处理是否正常\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500     U        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250     U        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500     U        S  "
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 特征工程\r\n",
    "### 3.2.1 特征提取\r\n",
    "数据分类：\r\n",
    "1. 数值类型\r\n",
    "2. 时间序列\r\n",
    "3. 分类数据"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "# 查看数据预处理后的信息，从中进行数据分类\r\n",
    "full.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        1309 non-null   object \n",
      " 11  Embarked     1309 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "source": [
    "# 数据分类情况\r\n",
    "'''\r\n",
    "1.数值类型：\r\n",
    "乘客编号（PassengerId），年龄（Age），船票价格（Fare），同代直系亲属人数（SibSp），不同代直系亲属人数（Parch）\r\n",
    "2.时间序列：无\r\n",
    "3.分类数据：\r\n",
    "1）有直接类别的\r\n",
    "乘客性别（Sex）：男性male，女性female\r\n",
    "登船港口（Embarked）：出发地点S=英国南安普顿Southampton，途径地点1：C=法国 瑟堡市Cherbourg，出发地点2：Q=爱尔兰 昆士敦Queenstown\r\n",
    "客舱等级（Pclass）：1=1等舱，2=2等舱，3=3等舱\r\n",
    "2）字符串类型：可能从这里面提取出特征来，也归到分类数据中\r\n",
    "乘客姓名（Name）\r\n",
    "客舱号（Cabin）\r\n",
    "船票编号（Ticket）\r\n",
    "'''"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'\\n1.数值类型：\\n乘客编号（PassengerId），年龄（Age），船票价格（Fare），同代直系亲属人数（SibSp），不同代直系亲属人数（Parch）\\n2.时间序列：无\\n3.分类数据：\\n1）有直接类别的\\n乘客性别（Sex）：男性male，女性female\\n登船港口（Embarked）：出发地点S=英国南安普顿Southampton，途径地点1：C=法国 瑟堡市Cherbourg，出发地点2：Q=爱尔兰 昆士敦Queenstown\\n客舱等级（Pclass）：1=1等舱，2=2等舱，3=3等舱\\n2）字符串类型：可能从这里面提取出特征来，也归到分类数据中\\n乘客姓名（Name）\\n客舱号（Cabin）\\n船票编号（Ticket）\\n'"
      ]
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 对分类数据进行处理：用数值代替类别，并进行One-hot编码\r\n",
    "### 分类数据：有直接类别的\r\n",
    "1. 乘客性别（Sex）：\r\n",
    "男性male，女性female\r\n",
    "2. 登船港口（Embarked）：出发地点S=英国南安普顿Southampton，途径地点1：C=法国 瑟堡市Cherbourg，出发地点2：Q=爱尔兰 昆士敦Queenstown\r\n",
    "3. 客舱等级（Pclass）：1=1等舱，2=2等舱，3=3等舱"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "乘客性别（Sex）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "# 查看性别数据这一列\r\n",
    "full['Sex'].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0      male\n",
       "1    female\n",
       "2    female\n",
       "3    female\n",
       "4      male\n",
       "Name: Sex, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "source": [
    "'''\r\n",
    "将性别的值映射为数值\r\n",
    "男（male）对应数值1，女（female）对应数值0\r\n",
    "'''\r\n",
    "sex_mapDict = {'male':1, 'female':0}\r\n",
    "\r\n",
    "# map函数：对Series每个数据应用自定义的函数计算  \r\n",
    "# 当只有两个参数时用 map 函数\r\n",
    "full['Sex'] = full['Sex'].map(sex_mapDict)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>U</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name  Sex   Age  SibSp  Parch  \\\n",
       "0                            Braund, Mr. Owen Harris    1  22.0      1      0   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...    0  38.0      1      0   \n",
       "2                             Heikkinen, Miss. Laina    0  26.0      0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)    0  35.0      1      0   \n",
       "4                           Allen, Mr. William Henry    1  35.0      0      0   \n",
       "\n",
       "             Ticket     Fare Cabin Embarked  \n",
       "0         A/5 21171   7.2500     U        S  \n",
       "1          PC 17599  71.2833   C85        C  \n",
       "2  STON/O2. 3101282   7.9250     U        S  \n",
       "3            113803  53.1000  C123        S  \n",
       "4            373450   8.0500     U        S  "
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "登船港口（Embarked）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "# 查看登船港口数据这一列\r\n",
    "full['Embarked'].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    S\n",
       "1    C\n",
       "2    S\n",
       "3    S\n",
       "4    S\n",
       "Name: Embarked, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Note:\r\n",
    "出发地点：S=英国南安普顿Southampton  \r\n",
    "途径地点1：C=法国 瑟堡市Cherbourg  \r\n",
    "途径地点2：Q=爱尔兰 昆士敦Queenstown"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "source": [
    "# 存放提取后的特征\r\n",
    "EmbarkedDF = pd.DataFrame()\r\n",
    "\r\n",
    "'''\r\n",
    "使用get_dummies进行one-hot编码，产生虚拟变量（dummy variables），列名前缀是Embarked\r\n",
    "'''\r\n",
    "EmbarkedDF = pd.get_dummies(full['Embarked'], prefix = 'Embarked')\r\n",
    "EmbarkedDF.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>Embarked_S</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Embarked_C  Embarked_Q  Embarked_S\n",
       "0           0           0           1\n",
       "1           1           0           0\n",
       "2           0           0           1\n",
       "3           0           0           1\n",
       "4           0           0           1"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "source": [
    "#添加one-hot编码产生的虚拟变量（dummy variables）到泰坦尼克号数据集full\r\n",
    "full = pd.concat([full, EmbarkedDF], axis=1)\r\n",
    "\r\n",
    "'''\r\n",
    "因为已经使用登船港口(Embarked)进行了one-hot编码产生了它的虚拟变量（dummy variables）\r\n",
    "所以这里把登船港口(Embarked)删掉\r\n",
    "'''\r\n",
    "full.drop('Embarked', axis=1, inplace=True)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>Embarked_S</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1       0.0       3   \n",
       "1            2       1.0       1   \n",
       "2            3       1.0       3   \n",
       "3            4       1.0       1   \n",
       "4            5       0.0       3   \n",
       "\n",
       "                                                Name  Sex   Age  SibSp  Parch  \\\n",
       "0                            Braund, Mr. Owen Harris    1  22.0      1      0   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...    0  38.0      1      0   \n",
       "2                             Heikkinen, Miss. Laina    0  26.0      0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)    0  35.0      1      0   \n",
       "4                           Allen, Mr. William Henry    1  35.0      0      0   \n",
       "\n",
       "             Ticket     Fare Cabin  Embarked_C  Embarked_Q  Embarked_S  \n",
       "0         A/5 21171   7.2500     U           0           0           1  \n",
       "1          PC 17599  71.2833   C85           1           0           0  \n",
       "2  STON/O2. 3101282   7.9250     U           0           0           1  \n",
       "3            113803  53.1000  C123           0           0           1  \n",
       "4            373450   8.0500     U           0           0           1  "
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "客舱等级（Pclass）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "source": [
    "'''\r\n",
    "客舱等级(Pclass):\r\n",
    "1=1等舱，2=2等舱，3=3等舱\r\n",
    "'''\r\n",
    "#存放提取后的特征\r\n",
    "PclassDf = pd.DataFrame()\r\n",
    "\r\n",
    "'''\r\n",
    "使用get_dummies进行one-hot编码，产生虚拟变量（dummy variables），列名前缀是Pclass\r\n",
    "'''\r\n",
    "PclassDF = pd.get_dummies(full['Pclass'], prefix = 'Pclass')\r\n",
    "PclassDF.head()\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pclass_1</th>\n",
       "      <th>Pclass_2</th>\n",
       "      <th>Pclass_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pclass_1  Pclass_2  Pclass_3\n",
       "0         0         0         1\n",
       "1         1         0         0\n",
       "2         0         0         1\n",
       "3         1         0         0\n",
       "4         0         0         1"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "source": [
    "# 添加one-hot编码产生的虚拟变量（dummy variables）到泰坦尼克号数据集full\r\n",
    "full = pd.concat([full, PclassDF], axis=1)\r\n",
    "\r\n",
    "'''\r\n",
    "因为已经使用客舱等级（Pclass）进行了one-hot编码产生了它的虚拟变量（dummy variables）\r\n",
    "所以这里把客舱等级（Pclass）删掉\r\n",
    "'''\r\n",
    "full.drop('Pclass', axis=1, inplace=True)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>Embarked_S</th>\n",
       "      <th>Pclass_1</th>\n",
       "      <th>Pclass_2</th>\n",
       "      <th>Pclass_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived                                               Name  \\\n",
       "0            1       0.0                            Braund, Mr. Owen Harris   \n",
       "1            2       1.0  Cumings, Mrs. John Bradley (Florence Briggs Th...   \n",
       "2            3       1.0                             Heikkinen, Miss. Laina   \n",
       "3            4       1.0       Futrelle, Mrs. Jacques Heath (Lily May Peel)   \n",
       "4            5       0.0                           Allen, Mr. William Henry   \n",
       "\n",
       "   Sex   Age  SibSp  Parch            Ticket     Fare Cabin  Embarked_C  \\\n",
       "0    1  22.0      1      0         A/5 21171   7.2500     U           0   \n",
       "1    0  38.0      1      0          PC 17599  71.2833   C85           1   \n",
       "2    0  26.0      0      0  STON/O2. 3101282   7.9250     U           0   \n",
       "3    0  35.0      1      0            113803  53.1000  C123           0   \n",
       "4    1  35.0      0      0            373450   8.0500     U           0   \n",
       "\n",
       "   Embarked_Q  Embarked_S  Pclass_1  Pclass_2  Pclass_3  \n",
       "0           0           1         0         0         1  \n",
       "1           0           0         1         0         0  \n",
       "2           0           1         0         0         1  \n",
       "3           0           1         1         0         0  \n",
       "4           0           1         0         0         1  "
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 分类数据：字符串类型\r\n",
    "字符串类型：可能从这里面提取出特征来，也归到分类数据中，这里数据有：\r\n",
    "\r\n",
    "1. 乘客姓名（Name）\r\n",
    "2. 客舱号（Cabin）\r\n",
    "3. 船票编号（Ticket）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "从姓名中提取头衔"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "source": [
    "'''\r\n",
    "查看姓名这一列长啥样\r\n",
    "注意到在乘客名字（Name）中，有一个非常显著的特点：\r\n",
    "乘客头衔每个名字当中都包含了具体的称谓或者说是头衔，将这部分信息提取出来后可以作为非常有用一个新变量，可以帮助我们进行预测。\r\n",
    "例如：\r\n",
    "Braund, Mr. Owen Harris\r\n",
    "Heikkinen, Miss. Laina\r\n",
    "Oliva y Ocana, Dona. Fermina\r\n",
    "Peter, Master. Michael J\r\n",
    "'''\r\n",
    "full[ 'Name' ].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0                              Braund, Mr. Owen Harris\n",
       "1    Cumings, Mrs. John Bradley (Florence Briggs Th...\n",
       "2                               Heikkinen, Miss. Laina\n",
       "3         Futrelle, Mrs. Jacques Heath (Lily May Peel)\n",
       "4                             Allen, Mr. William Henry\n",
       "Name: Name, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "source": [
    "'''\r\n",
    "定义函数：从姓名中获取头衔\r\n",
    "'''\r\n",
    "def getTitle(name):\r\n",
    "    str1 = name.split(',')[1] # 例如：Mr. Owen Harris\r\n",
    "    str2 = str1.split('.')[0] # 例如：Mr\r\n",
    "    str3 = str2.strip()       # strip() 方法用于移除字符串头尾指定的字符（默认为空格）\r\n",
    "    return str3"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "source": [
    "# 存放提取后的姓名头衔的特征\r\n",
    "titleDF = pd.DataFrame()\r\n",
    "\r\n",
    "# map函数：对Series每个数据应用自定义的函数计算\r\n",
    "titleDF['Title'] = full['Name'].map(getTitle)\r\n",
    "titleDF.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Title</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Mr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Mrs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Miss</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Mrs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Mr</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Title\n",
       "0    Mr\n",
       "1   Mrs\n",
       "2  Miss\n",
       "3   Mrs\n",
       "4    Mr"
      ]
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "source": [
    "'''\r\n",
    "定义以下几种头衔类别：\r\n",
    "Officer政府官员\r\n",
    "Royalty王室（皇室）\r\n",
    "Mr已婚男士\r\n",
    "Mrs已婚妇女\r\n",
    "Miss年轻未婚女子\r\n",
    "Master有技能的人/教师\r\n",
    "'''\r\n",
    "# 姓名中头衔字符串与定义头衔类别的映射关系\r\n",
    "title_mapDict = {\r\n",
    "                    \"Capt\":       \"Officer\",\r\n",
    "                    \"Col\":        \"Officer\",\r\n",
    "                    \"Major\":      \"Officer\",\r\n",
    "                    \"Jonkheer\":   \"Royalty\",\r\n",
    "                    \"Don\":        \"Royalty\",\r\n",
    "                    \"Sir\" :       \"Royalty\",\r\n",
    "                    \"Dr\":         \"Officer\",\r\n",
    "                    \"Rev\":        \"Officer\",\r\n",
    "                    \"the Countess\":\"Royalty\",\r\n",
    "                    \"Dona\":       \"Royalty\",\r\n",
    "                    \"Mme\":        \"Mrs\",\r\n",
    "                    \"Mlle\":       \"Miss\",\r\n",
    "                    \"Ms\":         \"Mrs\",\r\n",
    "                    \"Mr\" :        \"Mr\",\r\n",
    "                    \"Mrs\" :       \"Mrs\",\r\n",
    "                    \"Miss\" :      \"Miss\",\r\n",
    "                    \"Master\" :    \"Master\",\r\n",
    "                    \"Lady\" :      \"Royalty\"\r\n",
    "                    }\r\n",
    "\r\n",
    "# map函数：对Series每个数据应用自定义的函数计算\r\n",
    "titleDF['Title'] = titleDF['Title'].map(title_mapDict)\r\n",
    "\r\n",
    "'''\r\n",
    "使用get_dummies进行one-hot编码，产生虚拟变量（dummy variables），列名前缀是Pclass\r\n",
    "'''\r\n",
    "titleDF = pd.get_dummies(titleDF['Title'])\r\n",
    "titleDF.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Master</th>\n",
       "      <th>Miss</th>\n",
       "      <th>Mr</th>\n",
       "      <th>Mrs</th>\n",
       "      <th>Officer</th>\n",
       "      <th>Royalty</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Master  Miss  Mr  Mrs  Officer  Royalty\n",
       "0       0     0   1    0        0        0\n",
       "1       0     0   0    1        0        0\n",
       "2       0     1   0    0        0        0\n",
       "3       0     0   0    1        0        0\n",
       "4       0     0   1    0        0        0"
      ]
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "source": [
    "# 添加one-hot编码产生的虚拟变量（dummy variables）到泰坦尼克号数据集full\r\n",
    "full = pd.concat([full, titleDF], axis=1)\r\n",
    "\r\n",
    "'''\r\n",
    "因为已经使用 姓名中头衔（titleDF）进行了one-hot编码产生了它的虚拟变量（dummy variables）\r\n",
    "所以这里把 姓名（Name）删掉\r\n",
    "'''\r\n",
    "full.drop('Name', axis=1, inplace=True)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>...</th>\n",
       "      <th>Embarked_S</th>\n",
       "      <th>Pclass_1</th>\n",
       "      <th>Pclass_2</th>\n",
       "      <th>Pclass_3</th>\n",
       "      <th>Master</th>\n",
       "      <th>Miss</th>\n",
       "      <th>Mr</th>\n",
       "      <th>Mrs</th>\n",
       "      <th>Officer</th>\n",
       "      <th>Royalty</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>U</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Sex   Age  SibSp  Parch            Ticket     Fare  \\\n",
       "0            1       0.0    1  22.0      1      0         A/5 21171   7.2500   \n",
       "1            2       1.0    0  38.0      1      0          PC 17599  71.2833   \n",
       "2            3       1.0    0  26.0      0      0  STON/O2. 3101282   7.9250   \n",
       "3            4       1.0    0  35.0      1      0            113803  53.1000   \n",
       "4            5       0.0    1  35.0      0      0            373450   8.0500   \n",
       "\n",
       "  Cabin  Embarked_C  ...  Embarked_S  Pclass_1  Pclass_2  Pclass_3  Master  \\\n",
       "0     U           0  ...           1         0         0         1       0   \n",
       "1   C85           1  ...           0         1         0         0       0   \n",
       "2     U           0  ...           1         0         0         1       0   \n",
       "3  C123           0  ...           1         1         0         0       0   \n",
       "4     U           0  ...           1         0         0         1       0   \n",
       "\n",
       "   Miss  Mr  Mrs  Officer  Royalty  \n",
       "0     0   1    0        0        0  \n",
       "1     0   0    1        0        0  \n",
       "2     1   0    0        0        0  \n",
       "3     0   0    1        0        0  \n",
       "4     0   1    0        0        0  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "从客舱号中提取客舱类别"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "source": [
    "'''\r\n",
    "客舱号的首字母是客舱的类别\r\n",
    "'''\r\n",
    "# 查看客舱号的内容\r\n",
    "full['Cabin'].head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0       U\n",
       "1     C85\n",
       "2       U\n",
       "3    C123\n",
       "4       U\n",
       "Name: Cabin, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "source": [
    "# 存放提取后的客舱号类别的特征\r\n",
    "CabinDF = pd.DataFrame()\r\n",
    "\r\n",
    "# map函数：对Series每个数据应用自定义的函数计算\r\n",
    "full['Cabin'] = full['Cabin'].map(lambda c: c[0])\r\n",
    "\r\n",
    "'''\r\n",
    "使用get_dummies进行one-hot编码，产生虚拟变量（dummy variables），列名前缀是Cabin\r\n",
    "'''\r\n",
    "CabinDF = pd.get_dummies(full['Cabin'], prefix='Cabin')\r\n",
    "CabinDF.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cabin_A</th>\n",
       "      <th>Cabin_B</th>\n",
       "      <th>Cabin_C</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_U</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Cabin_A  Cabin_B  Cabin_C  Cabin_D  Cabin_E  Cabin_F  Cabin_G  Cabin_T  \\\n",
       "0        0        0        0        0        0        0        0        0   \n",
       "1        0        0        1        0        0        0        0        0   \n",
       "2        0        0        0        0        0        0        0        0   \n",
       "3        0        0        1        0        0        0        0        0   \n",
       "4        0        0        0        0        0        0        0        0   \n",
       "\n",
       "   Cabin_U  \n",
       "0        1  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "source": [
    "# 添加one-hot编码产生的虚拟变量（dummy variables）到泰坦尼克号数据集full\r\n",
    "full = pd.concat([full, CabinDF], axis=1)\r\n",
    "\r\n",
    "'''\r\n",
    "因为已经使用 客舱类别（CabinDF）进行了one-hot编码产生了它的虚拟变量（dummy variables）\r\n",
    "所以这里把 客舱号（Cabin）删掉\r\n",
    "'''\r\n",
    "full.drop('Cabin', axis=1, inplace=True)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>...</th>\n",
       "      <th>Royalty</th>\n",
       "      <th>Cabin_A</th>\n",
       "      <th>Cabin_B</th>\n",
       "      <th>Cabin_C</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_U</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 29 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Sex   Age  SibSp  Parch            Ticket     Fare  \\\n",
       "0            1       0.0    1  22.0      1      0         A/5 21171   7.2500   \n",
       "1            2       1.0    0  38.0      1      0          PC 17599  71.2833   \n",
       "2            3       1.0    0  26.0      0      0  STON/O2. 3101282   7.9250   \n",
       "3            4       1.0    0  35.0      1      0            113803  53.1000   \n",
       "4            5       0.0    1  35.0      0      0            373450   8.0500   \n",
       "\n",
       "   Embarked_C  Embarked_Q  ...  Royalty  Cabin_A  Cabin_B  Cabin_C  Cabin_D  \\\n",
       "0           0           0  ...        0        0        0        0        0   \n",
       "1           1           0  ...        0        0        0        1        0   \n",
       "2           0           0  ...        0        0        0        0        0   \n",
       "3           0           0  ...        0        0        0        1        0   \n",
       "4           0           0  ...        0        0        0        0        0   \n",
       "\n",
       "   Cabin_E  Cabin_F  Cabin_G  Cabin_T  Cabin_U  \n",
       "0        0        0        0        0        1  \n",
       "1        0        0        0        0        0  \n",
       "2        0        0        0        0        1  \n",
       "3        0        0        0        0        0  \n",
       "4        0        0        0        0        1  \n",
       "\n",
       "[5 rows x 29 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 对数值类型进行处理\r\n",
    "建立家庭人数和家庭类别"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "source": [
    "# 存放家庭信息特征\r\n",
    "familyDF = pd.DataFrame()\r\n",
    "\r\n",
    "'''\r\n",
    "家庭人数 = 同代直系亲属数（Parch）+ 不同代直系亲属数（SibSp）+ 乘客自己\r\n",
    "（因为乘客自己也是家庭成员的一个，所以这里加1）\r\n",
    "'''\r\n",
    "familyDF['FamilySize'] = full['Parch'] + full['SibSp'] + 1\r\n",
    "\r\n",
    "'''\r\n",
    "家庭类别：\r\n",
    "小家庭Family_Single：家庭人数=1\r\n",
    "中等家庭Family_Small: 2<=家庭人数<=4\r\n",
    "大家庭Family_Large: 家庭人数>=5\r\n",
    "'''\r\n",
    "# if条件为真的时候返回if前面内容，否则返回0\r\n",
    "familyDF['Family_Single'] = familyDF['FamilySize'].map(lambda s: 1 if s ==1 else 0)\r\n",
    "familyDF['Family_Small'] = familyDF['FamilySize'].map(lambda s: 1 if 2 <= s <=4 else 0)\r\n",
    "familyDF['Family_Large'] = familyDF['FamilySize'].map(lambda s: 1 if 5 <= s else 0)\r\n",
    "\r\n",
    "familyDF.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>FamilySize</th>\n",
       "      <th>Family_Single</th>\n",
       "      <th>Family_Small</th>\n",
       "      <th>Family_Large</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   FamilySize  Family_Single  Family_Small  Family_Large\n",
       "0           2              0             1             0\n",
       "1           2              0             1             0\n",
       "2           1              1             0             0\n",
       "3           2              0             1             0\n",
       "4           1              1             0             0"
      ]
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "source": [
    "# 添加one-hot编码产生的虚拟变量（dummy variables）到泰坦尼克号数据集full\r\n",
    "full = pd.concat([full, familyDF], axis=1)\r\n",
    "full.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>...</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_U</th>\n",
       "      <th>FamilySize</th>\n",
       "      <th>Family_Single</th>\n",
       "      <th>Family_Small</th>\n",
       "      <th>Family_Large</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Sex   Age  SibSp  Parch            Ticket     Fare  \\\n",
       "0            1       0.0    1  22.0      1      0         A/5 21171   7.2500   \n",
       "1            2       1.0    0  38.0      1      0          PC 17599  71.2833   \n",
       "2            3       1.0    0  26.0      0      0  STON/O2. 3101282   7.9250   \n",
       "3            4       1.0    0  35.0      1      0            113803  53.1000   \n",
       "4            5       0.0    1  35.0      0      0            373450   8.0500   \n",
       "\n",
       "   Embarked_C  Embarked_Q  ...  Cabin_D  Cabin_E  Cabin_F  Cabin_G  Cabin_T  \\\n",
       "0           0           0  ...        0        0        0        0        0   \n",
       "1           1           0  ...        0        0        0        0        0   \n",
       "2           0           0  ...        0        0        0        0        0   \n",
       "3           0           0  ...        0        0        0        0        0   \n",
       "4           0           0  ...        0        0        0        0        0   \n",
       "\n",
       "   Cabin_U  FamilySize  Family_Single  Family_Small  Family_Large  \n",
       "0        1           2              0             1             0  \n",
       "1        0           2              0             1             0  \n",
       "2        1           1              1             0             0  \n",
       "3        0           2              0             1             0  \n",
       "4        1           1              1             0             0  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "source": [
    "# 到现在我们已经有了这么多个特征了\r\n",
    "full.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(1309, 33)"
      ]
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.2.2 特征选择"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "相关系数法：计算各个特征的相关系数"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "source": [
    "# 相关性矩阵\r\n",
    "corrDF = full.corr() # 相关系数\r\n",
    "corrDF"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>Embarked_S</th>\n",
       "      <th>...</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_U</th>\n",
       "      <th>FamilySize</th>\n",
       "      <th>Family_Single</th>\n",
       "      <th>Family_Small</th>\n",
       "      <th>Family_Large</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.005007</td>\n",
       "      <td>0.013406</td>\n",
       "      <td>0.025731</td>\n",
       "      <td>-0.055224</td>\n",
       "      <td>0.008942</td>\n",
       "      <td>0.031416</td>\n",
       "      <td>0.048101</td>\n",
       "      <td>0.011585</td>\n",
       "      <td>-0.049836</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000549</td>\n",
       "      <td>-0.008136</td>\n",
       "      <td>0.000306</td>\n",
       "      <td>-0.045949</td>\n",
       "      <td>-0.023049</td>\n",
       "      <td>0.000208</td>\n",
       "      <td>-0.031437</td>\n",
       "      <td>0.028546</td>\n",
       "      <td>0.002975</td>\n",
       "      <td>-0.063415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Survived</th>\n",
       "      <td>-0.005007</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.543351</td>\n",
       "      <td>-0.070323</td>\n",
       "      <td>-0.035322</td>\n",
       "      <td>0.081629</td>\n",
       "      <td>0.257307</td>\n",
       "      <td>0.168240</td>\n",
       "      <td>0.003650</td>\n",
       "      <td>-0.149683</td>\n",
       "      <td>...</td>\n",
       "      <td>0.150716</td>\n",
       "      <td>0.145321</td>\n",
       "      <td>0.057935</td>\n",
       "      <td>0.016040</td>\n",
       "      <td>-0.026456</td>\n",
       "      <td>-0.316912</td>\n",
       "      <td>0.016639</td>\n",
       "      <td>-0.203367</td>\n",
       "      <td>0.279855</td>\n",
       "      <td>-0.125147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sex</th>\n",
       "      <td>0.013406</td>\n",
       "      <td>-0.543351</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.057397</td>\n",
       "      <td>-0.109609</td>\n",
       "      <td>-0.213125</td>\n",
       "      <td>-0.185484</td>\n",
       "      <td>-0.066564</td>\n",
       "      <td>-0.088651</td>\n",
       "      <td>0.115193</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.057396</td>\n",
       "      <td>-0.040340</td>\n",
       "      <td>-0.006655</td>\n",
       "      <td>-0.083285</td>\n",
       "      <td>0.020558</td>\n",
       "      <td>0.137396</td>\n",
       "      <td>-0.188583</td>\n",
       "      <td>0.284537</td>\n",
       "      <td>-0.255196</td>\n",
       "      <td>-0.077748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Age</th>\n",
       "      <td>0.025731</td>\n",
       "      <td>-0.070323</td>\n",
       "      <td>0.057397</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.190747</td>\n",
       "      <td>-0.130872</td>\n",
       "      <td>0.171521</td>\n",
       "      <td>0.076179</td>\n",
       "      <td>-0.012718</td>\n",
       "      <td>-0.059153</td>\n",
       "      <td>...</td>\n",
       "      <td>0.132886</td>\n",
       "      <td>0.106600</td>\n",
       "      <td>-0.072644</td>\n",
       "      <td>-0.085977</td>\n",
       "      <td>0.032461</td>\n",
       "      <td>-0.271918</td>\n",
       "      <td>-0.196996</td>\n",
       "      <td>0.116675</td>\n",
       "      <td>-0.038189</td>\n",
       "      <td>-0.161210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SibSp</th>\n",
       "      <td>-0.055224</td>\n",
       "      <td>-0.035322</td>\n",
       "      <td>-0.109609</td>\n",
       "      <td>-0.190747</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.373587</td>\n",
       "      <td>0.160224</td>\n",
       "      <td>-0.048396</td>\n",
       "      <td>-0.048678</td>\n",
       "      <td>0.073709</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015727</td>\n",
       "      <td>-0.027180</td>\n",
       "      <td>-0.008619</td>\n",
       "      <td>0.006015</td>\n",
       "      <td>-0.013247</td>\n",
       "      <td>0.009064</td>\n",
       "      <td>0.861952</td>\n",
       "      <td>-0.591077</td>\n",
       "      <td>0.253590</td>\n",
       "      <td>0.699681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Parch</th>\n",
       "      <td>0.008942</td>\n",
       "      <td>0.081629</td>\n",
       "      <td>-0.213125</td>\n",
       "      <td>-0.130872</td>\n",
       "      <td>0.373587</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.221522</td>\n",
       "      <td>-0.008635</td>\n",
       "      <td>-0.100943</td>\n",
       "      <td>0.071881</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.027385</td>\n",
       "      <td>0.001084</td>\n",
       "      <td>0.020481</td>\n",
       "      <td>0.058325</td>\n",
       "      <td>-0.012304</td>\n",
       "      <td>-0.036806</td>\n",
       "      <td>0.792296</td>\n",
       "      <td>-0.549022</td>\n",
       "      <td>0.248532</td>\n",
       "      <td>0.624627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fare</th>\n",
       "      <td>0.031416</td>\n",
       "      <td>0.257307</td>\n",
       "      <td>-0.185484</td>\n",
       "      <td>0.171521</td>\n",
       "      <td>0.160224</td>\n",
       "      <td>0.221522</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.286241</td>\n",
       "      <td>-0.130054</td>\n",
       "      <td>-0.169894</td>\n",
       "      <td>...</td>\n",
       "      <td>0.072737</td>\n",
       "      <td>0.073949</td>\n",
       "      <td>-0.037567</td>\n",
       "      <td>-0.022857</td>\n",
       "      <td>0.001179</td>\n",
       "      <td>-0.507197</td>\n",
       "      <td>0.226465</td>\n",
       "      <td>-0.274826</td>\n",
       "      <td>0.197281</td>\n",
       "      <td>0.170853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Embarked_C</th>\n",
       "      <td>0.048101</td>\n",
       "      <td>0.168240</td>\n",
       "      <td>-0.066564</td>\n",
       "      <td>0.076179</td>\n",
       "      <td>-0.048396</td>\n",
       "      <td>-0.008635</td>\n",
       "      <td>0.286241</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.164166</td>\n",
       "      <td>-0.778262</td>\n",
       "      <td>...</td>\n",
       "      <td>0.107782</td>\n",
       "      <td>0.027566</td>\n",
       "      <td>-0.020010</td>\n",
       "      <td>-0.031566</td>\n",
       "      <td>-0.014095</td>\n",
       "      <td>-0.258257</td>\n",
       "      <td>-0.036553</td>\n",
       "      <td>-0.107874</td>\n",
       "      <td>0.159594</td>\n",
       "      <td>-0.092825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Embarked_Q</th>\n",
       "      <td>0.011585</td>\n",
       "      <td>0.003650</td>\n",
       "      <td>-0.088651</td>\n",
       "      <td>-0.012718</td>\n",
       "      <td>-0.048678</td>\n",
       "      <td>-0.100943</td>\n",
       "      <td>-0.130054</td>\n",
       "      <td>-0.164166</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.491656</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.061459</td>\n",
       "      <td>-0.042877</td>\n",
       "      <td>-0.020282</td>\n",
       "      <td>-0.019941</td>\n",
       "      <td>-0.008904</td>\n",
       "      <td>0.142369</td>\n",
       "      <td>-0.087190</td>\n",
       "      <td>0.127214</td>\n",
       "      <td>-0.122491</td>\n",
       "      <td>-0.018423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Embarked_S</th>\n",
       "      <td>-0.049836</td>\n",
       "      <td>-0.149683</td>\n",
       "      <td>0.115193</td>\n",
       "      <td>-0.059153</td>\n",
       "      <td>0.073709</td>\n",
       "      <td>0.071881</td>\n",
       "      <td>-0.169894</td>\n",
       "      <td>-0.778262</td>\n",
       "      <td>-0.491656</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.056023</td>\n",
       "      <td>0.002960</td>\n",
       "      <td>0.030575</td>\n",
       "      <td>0.040560</td>\n",
       "      <td>0.018111</td>\n",
       "      <td>0.137351</td>\n",
       "      <td>0.087771</td>\n",
       "      <td>0.014246</td>\n",
       "      <td>-0.062909</td>\n",
       "      <td>0.093671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pclass_1</th>\n",
       "      <td>0.026495</td>\n",
       "      <td>0.285904</td>\n",
       "      <td>-0.107371</td>\n",
       "      <td>0.362587</td>\n",
       "      <td>-0.034256</td>\n",
       "      <td>-0.013033</td>\n",
       "      <td>0.599956</td>\n",
       "      <td>0.325722</td>\n",
       "      <td>-0.166101</td>\n",
       "      <td>-0.181800</td>\n",
       "      <td>...</td>\n",
       "      <td>0.275698</td>\n",
       "      <td>0.242963</td>\n",
       "      <td>-0.073083</td>\n",
       "      <td>-0.035441</td>\n",
       "      <td>0.048310</td>\n",
       "      <td>-0.776987</td>\n",
       "      <td>-0.029656</td>\n",
       "      <td>-0.126551</td>\n",
       "      <td>0.165965</td>\n",
       "      <td>-0.067523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pclass_2</th>\n",
       "      <td>0.022714</td>\n",
       "      <td>0.093349</td>\n",
       "      <td>-0.028862</td>\n",
       "      <td>-0.014193</td>\n",
       "      <td>-0.052419</td>\n",
       "      <td>-0.010057</td>\n",
       "      <td>-0.121372</td>\n",
       "      <td>-0.134675</td>\n",
       "      <td>-0.121973</td>\n",
       "      <td>0.196532</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.037929</td>\n",
       "      <td>-0.050210</td>\n",
       "      <td>0.127371</td>\n",
       "      <td>-0.032081</td>\n",
       "      <td>-0.014325</td>\n",
       "      <td>0.176485</td>\n",
       "      <td>-0.039976</td>\n",
       "      <td>-0.035075</td>\n",
       "      <td>0.097270</td>\n",
       "      <td>-0.118495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pclass_3</th>\n",
       "      <td>-0.041544</td>\n",
       "      <td>-0.322308</td>\n",
       "      <td>0.116562</td>\n",
       "      <td>-0.302093</td>\n",
       "      <td>0.072610</td>\n",
       "      <td>0.019521</td>\n",
       "      <td>-0.419616</td>\n",
       "      <td>-0.171430</td>\n",
       "      <td>0.243706</td>\n",
       "      <td>-0.003805</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.207455</td>\n",
       "      <td>-0.169063</td>\n",
       "      <td>-0.041178</td>\n",
       "      <td>0.056964</td>\n",
       "      <td>-0.030057</td>\n",
       "      <td>0.527614</td>\n",
       "      <td>0.058430</td>\n",
       "      <td>0.138250</td>\n",
       "      <td>-0.223338</td>\n",
       "      <td>0.155560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Master</th>\n",
       "      <td>0.002254</td>\n",
       "      <td>0.085221</td>\n",
       "      <td>0.164375</td>\n",
       "      <td>-0.363923</td>\n",
       "      <td>0.329171</td>\n",
       "      <td>0.253482</td>\n",
       "      <td>0.011596</td>\n",
       "      <td>-0.014172</td>\n",
       "      <td>-0.009091</td>\n",
       "      <td>0.018297</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.042192</td>\n",
       "      <td>0.001860</td>\n",
       "      <td>0.058311</td>\n",
       "      <td>-0.013690</td>\n",
       "      <td>-0.006113</td>\n",
       "      <td>0.041178</td>\n",
       "      <td>0.355061</td>\n",
       "      <td>-0.265355</td>\n",
       "      <td>0.120166</td>\n",
       "      <td>0.301809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miss</th>\n",
       "      <td>-0.050027</td>\n",
       "      <td>0.332795</td>\n",
       "      <td>-0.672819</td>\n",
       "      <td>-0.254146</td>\n",
       "      <td>0.077564</td>\n",
       "      <td>0.066473</td>\n",
       "      <td>0.092051</td>\n",
       "      <td>-0.014351</td>\n",
       "      <td>0.198804</td>\n",
       "      <td>-0.113886</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.012516</td>\n",
       "      <td>0.008700</td>\n",
       "      <td>-0.003088</td>\n",
       "      <td>0.061881</td>\n",
       "      <td>-0.013832</td>\n",
       "      <td>-0.004364</td>\n",
       "      <td>0.087350</td>\n",
       "      <td>-0.023890</td>\n",
       "      <td>-0.018085</td>\n",
       "      <td>0.083422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mr</th>\n",
       "      <td>0.014116</td>\n",
       "      <td>-0.549199</td>\n",
       "      <td>0.870678</td>\n",
       "      <td>0.165476</td>\n",
       "      <td>-0.243104</td>\n",
       "      <td>-0.304780</td>\n",
       "      <td>-0.192192</td>\n",
       "      <td>-0.065538</td>\n",
       "      <td>-0.080224</td>\n",
       "      <td>0.108924</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.030261</td>\n",
       "      <td>-0.032953</td>\n",
       "      <td>-0.026403</td>\n",
       "      <td>-0.072514</td>\n",
       "      <td>0.023611</td>\n",
       "      <td>0.131807</td>\n",
       "      <td>-0.326487</td>\n",
       "      <td>0.386262</td>\n",
       "      <td>-0.300872</td>\n",
       "      <td>-0.194207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mrs</th>\n",
       "      <td>0.033299</td>\n",
       "      <td>0.344935</td>\n",
       "      <td>-0.571176</td>\n",
       "      <td>0.198091</td>\n",
       "      <td>0.061643</td>\n",
       "      <td>0.213491</td>\n",
       "      <td>0.139235</td>\n",
       "      <td>0.098379</td>\n",
       "      <td>-0.100374</td>\n",
       "      <td>-0.022950</td>\n",
       "      <td>...</td>\n",
       "      <td>0.080393</td>\n",
       "      <td>0.045538</td>\n",
       "      <td>0.013376</td>\n",
       "      <td>0.042547</td>\n",
       "      <td>-0.011742</td>\n",
       "      <td>-0.162253</td>\n",
       "      <td>0.157233</td>\n",
       "      <td>-0.354649</td>\n",
       "      <td>0.361247</td>\n",
       "      <td>0.012893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Officer</th>\n",
       "      <td>0.002231</td>\n",
       "      <td>-0.031316</td>\n",
       "      <td>0.087288</td>\n",
       "      <td>0.162818</td>\n",
       "      <td>-0.013813</td>\n",
       "      <td>-0.032631</td>\n",
       "      <td>0.028696</td>\n",
       "      <td>0.003678</td>\n",
       "      <td>-0.003212</td>\n",
       "      <td>-0.001202</td>\n",
       "      <td>...</td>\n",
       "      <td>0.006055</td>\n",
       "      <td>-0.024048</td>\n",
       "      <td>-0.017076</td>\n",
       "      <td>-0.008281</td>\n",
       "      <td>-0.003698</td>\n",
       "      <td>-0.067030</td>\n",
       "      <td>-0.026921</td>\n",
       "      <td>0.013303</td>\n",
       "      <td>0.003966</td>\n",
       "      <td>-0.034572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Royalty</th>\n",
       "      <td>0.004400</td>\n",
       "      <td>0.033391</td>\n",
       "      <td>-0.020408</td>\n",
       "      <td>0.059466</td>\n",
       "      <td>-0.010787</td>\n",
       "      <td>-0.030197</td>\n",
       "      <td>0.026214</td>\n",
       "      <td>0.077213</td>\n",
       "      <td>-0.021853</td>\n",
       "      <td>-0.054250</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.012950</td>\n",
       "      <td>-0.012202</td>\n",
       "      <td>-0.008665</td>\n",
       "      <td>-0.004202</td>\n",
       "      <td>-0.001876</td>\n",
       "      <td>-0.071672</td>\n",
       "      <td>-0.023600</td>\n",
       "      <td>0.008761</td>\n",
       "      <td>-0.000073</td>\n",
       "      <td>-0.017542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_A</th>\n",
       "      <td>-0.002831</td>\n",
       "      <td>0.022287</td>\n",
       "      <td>0.047561</td>\n",
       "      <td>0.125177</td>\n",
       "      <td>-0.039808</td>\n",
       "      <td>-0.030707</td>\n",
       "      <td>0.020094</td>\n",
       "      <td>0.094914</td>\n",
       "      <td>-0.042105</td>\n",
       "      <td>-0.056984</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.024952</td>\n",
       "      <td>-0.023510</td>\n",
       "      <td>-0.016695</td>\n",
       "      <td>-0.008096</td>\n",
       "      <td>-0.003615</td>\n",
       "      <td>-0.242399</td>\n",
       "      <td>-0.042967</td>\n",
       "      <td>0.045227</td>\n",
       "      <td>-0.029546</td>\n",
       "      <td>-0.033799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_B</th>\n",
       "      <td>0.015895</td>\n",
       "      <td>0.175095</td>\n",
       "      <td>-0.094453</td>\n",
       "      <td>0.113458</td>\n",
       "      <td>-0.011569</td>\n",
       "      <td>0.073051</td>\n",
       "      <td>0.393743</td>\n",
       "      <td>0.161595</td>\n",
       "      <td>-0.073613</td>\n",
       "      <td>-0.095790</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.043624</td>\n",
       "      <td>-0.041103</td>\n",
       "      <td>-0.029188</td>\n",
       "      <td>-0.014154</td>\n",
       "      <td>-0.006320</td>\n",
       "      <td>-0.423794</td>\n",
       "      <td>0.032318</td>\n",
       "      <td>-0.087912</td>\n",
       "      <td>0.084268</td>\n",
       "      <td>0.013470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_C</th>\n",
       "      <td>0.006092</td>\n",
       "      <td>0.114652</td>\n",
       "      <td>-0.077473</td>\n",
       "      <td>0.167993</td>\n",
       "      <td>0.048616</td>\n",
       "      <td>0.009601</td>\n",
       "      <td>0.401370</td>\n",
       "      <td>0.158043</td>\n",
       "      <td>-0.059151</td>\n",
       "      <td>-0.101861</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.053083</td>\n",
       "      <td>-0.050016</td>\n",
       "      <td>-0.035516</td>\n",
       "      <td>-0.017224</td>\n",
       "      <td>-0.007691</td>\n",
       "      <td>-0.515684</td>\n",
       "      <td>0.037226</td>\n",
       "      <td>-0.137498</td>\n",
       "      <td>0.141925</td>\n",
       "      <td>0.001362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_D</th>\n",
       "      <td>0.000549</td>\n",
       "      <td>0.150716</td>\n",
       "      <td>-0.057396</td>\n",
       "      <td>0.132886</td>\n",
       "      <td>-0.015727</td>\n",
       "      <td>-0.027385</td>\n",
       "      <td>0.072737</td>\n",
       "      <td>0.107782</td>\n",
       "      <td>-0.061459</td>\n",
       "      <td>-0.056023</td>\n",
       "      <td>...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.034317</td>\n",
       "      <td>-0.024369</td>\n",
       "      <td>-0.011817</td>\n",
       "      <td>-0.005277</td>\n",
       "      <td>-0.353822</td>\n",
       "      <td>-0.025313</td>\n",
       "      <td>-0.074310</td>\n",
       "      <td>0.102432</td>\n",
       "      <td>-0.049336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_E</th>\n",
       "      <td>-0.008136</td>\n",
       "      <td>0.145321</td>\n",
       "      <td>-0.040340</td>\n",
       "      <td>0.106600</td>\n",
       "      <td>-0.027180</td>\n",
       "      <td>0.001084</td>\n",
       "      <td>0.073949</td>\n",
       "      <td>0.027566</td>\n",
       "      <td>-0.042877</td>\n",
       "      <td>0.002960</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.034317</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.022961</td>\n",
       "      <td>-0.011135</td>\n",
       "      <td>-0.004972</td>\n",
       "      <td>-0.333381</td>\n",
       "      <td>-0.017285</td>\n",
       "      <td>-0.042535</td>\n",
       "      <td>0.068007</td>\n",
       "      <td>-0.046485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_F</th>\n",
       "      <td>0.000306</td>\n",
       "      <td>0.057935</td>\n",
       "      <td>-0.006655</td>\n",
       "      <td>-0.072644</td>\n",
       "      <td>-0.008619</td>\n",
       "      <td>0.020481</td>\n",
       "      <td>-0.037567</td>\n",
       "      <td>-0.020010</td>\n",
       "      <td>-0.020282</td>\n",
       "      <td>0.030575</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.024369</td>\n",
       "      <td>-0.022961</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.007907</td>\n",
       "      <td>-0.003531</td>\n",
       "      <td>-0.236733</td>\n",
       "      <td>0.005525</td>\n",
       "      <td>0.004055</td>\n",
       "      <td>0.012756</td>\n",
       "      <td>-0.033009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_G</th>\n",
       "      <td>-0.045949</td>\n",
       "      <td>0.016040</td>\n",
       "      <td>-0.083285</td>\n",
       "      <td>-0.085977</td>\n",
       "      <td>0.006015</td>\n",
       "      <td>0.058325</td>\n",
       "      <td>-0.022857</td>\n",
       "      <td>-0.031566</td>\n",
       "      <td>-0.019941</td>\n",
       "      <td>0.040560</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.011817</td>\n",
       "      <td>-0.011135</td>\n",
       "      <td>-0.007907</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.001712</td>\n",
       "      <td>-0.114803</td>\n",
       "      <td>0.035835</td>\n",
       "      <td>-0.076397</td>\n",
       "      <td>0.087471</td>\n",
       "      <td>-0.016008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_T</th>\n",
       "      <td>-0.023049</td>\n",
       "      <td>-0.026456</td>\n",
       "      <td>0.020558</td>\n",
       "      <td>0.032461</td>\n",
       "      <td>-0.013247</td>\n",
       "      <td>-0.012304</td>\n",
       "      <td>0.001179</td>\n",
       "      <td>-0.014095</td>\n",
       "      <td>-0.008904</td>\n",
       "      <td>0.018111</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005277</td>\n",
       "      <td>-0.004972</td>\n",
       "      <td>-0.003531</td>\n",
       "      <td>-0.001712</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.051263</td>\n",
       "      <td>-0.015438</td>\n",
       "      <td>0.022411</td>\n",
       "      <td>-0.019574</td>\n",
       "      <td>-0.007148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cabin_U</th>\n",
       "      <td>0.000208</td>\n",
       "      <td>-0.316912</td>\n",
       "      <td>0.137396</td>\n",
       "      <td>-0.271918</td>\n",
       "      <td>0.009064</td>\n",
       "      <td>-0.036806</td>\n",
       "      <td>-0.507197</td>\n",
       "      <td>-0.258257</td>\n",
       "      <td>0.142369</td>\n",
       "      <td>0.137351</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.353822</td>\n",
       "      <td>-0.333381</td>\n",
       "      <td>-0.236733</td>\n",
       "      <td>-0.114803</td>\n",
       "      <td>-0.051263</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.014155</td>\n",
       "      <td>0.175812</td>\n",
       "      <td>-0.211367</td>\n",
       "      <td>0.056438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FamilySize</th>\n",
       "      <td>-0.031437</td>\n",
       "      <td>0.016639</td>\n",
       "      <td>-0.188583</td>\n",
       "      <td>-0.196996</td>\n",
       "      <td>0.861952</td>\n",
       "      <td>0.792296</td>\n",
       "      <td>0.226465</td>\n",
       "      <td>-0.036553</td>\n",
       "      <td>-0.087190</td>\n",
       "      <td>0.087771</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.025313</td>\n",
       "      <td>-0.017285</td>\n",
       "      <td>0.005525</td>\n",
       "      <td>0.035835</td>\n",
       "      <td>-0.015438</td>\n",
       "      <td>-0.014155</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.688864</td>\n",
       "      <td>0.302640</td>\n",
       "      <td>0.801623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Family_Single</th>\n",
       "      <td>0.028546</td>\n",
       "      <td>-0.203367</td>\n",
       "      <td>0.284537</td>\n",
       "      <td>0.116675</td>\n",
       "      <td>-0.591077</td>\n",
       "      <td>-0.549022</td>\n",
       "      <td>-0.274826</td>\n",
       "      <td>-0.107874</td>\n",
       "      <td>0.127214</td>\n",
       "      <td>0.014246</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.074310</td>\n",
       "      <td>-0.042535</td>\n",
       "      <td>0.004055</td>\n",
       "      <td>-0.076397</td>\n",
       "      <td>0.022411</td>\n",
       "      <td>0.175812</td>\n",
       "      <td>-0.688864</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.873398</td>\n",
       "      <td>-0.318944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Family_Small</th>\n",
       "      <td>0.002975</td>\n",
       "      <td>0.279855</td>\n",
       "      <td>-0.255196</td>\n",
       "      <td>-0.038189</td>\n",
       "      <td>0.253590</td>\n",
       "      <td>0.248532</td>\n",
       "      <td>0.197281</td>\n",
       "      <td>0.159594</td>\n",
       "      <td>-0.122491</td>\n",
       "      <td>-0.062909</td>\n",
       "      <td>...</td>\n",
       "      <td>0.102432</td>\n",
       "      <td>0.068007</td>\n",
       "      <td>0.012756</td>\n",
       "      <td>0.087471</td>\n",
       "      <td>-0.019574</td>\n",
       "      <td>-0.211367</td>\n",
       "      <td>0.302640</td>\n",
       "      <td>-0.873398</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.183007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Family_Large</th>\n",
       "      <td>-0.063415</td>\n",
       "      <td>-0.125147</td>\n",
       "      <td>-0.077748</td>\n",
       "      <td>-0.161210</td>\n",
       "      <td>0.699681</td>\n",
       "      <td>0.624627</td>\n",
       "      <td>0.170853</td>\n",
       "      <td>-0.092825</td>\n",
       "      <td>-0.018423</td>\n",
       "      <td>0.093671</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.049336</td>\n",
       "      <td>-0.046485</td>\n",
       "      <td>-0.033009</td>\n",
       "      <td>-0.016008</td>\n",
       "      <td>-0.007148</td>\n",
       "      <td>0.056438</td>\n",
       "      <td>0.801623</td>\n",
       "      <td>-0.318944</td>\n",
       "      <td>-0.183007</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>32 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               PassengerId  Survived       Sex       Age     SibSp     Parch  \\\n",
       "PassengerId       1.000000 -0.005007  0.013406  0.025731 -0.055224  0.008942   \n",
       "Survived         -0.005007  1.000000 -0.543351 -0.070323 -0.035322  0.081629   \n",
       "Sex               0.013406 -0.543351  1.000000  0.057397 -0.109609 -0.213125   \n",
       "Age               0.025731 -0.070323  0.057397  1.000000 -0.190747 -0.130872   \n",
       "SibSp            -0.055224 -0.035322 -0.109609 -0.190747  1.000000  0.373587   \n",
       "Parch             0.008942  0.081629 -0.213125 -0.130872  0.373587  1.000000   \n",
       "Fare              0.031416  0.257307 -0.185484  0.171521  0.160224  0.221522   \n",
       "Embarked_C        0.048101  0.168240 -0.066564  0.076179 -0.048396 -0.008635   \n",
       "Embarked_Q        0.011585  0.003650 -0.088651 -0.012718 -0.048678 -0.100943   \n",
       "Embarked_S       -0.049836 -0.149683  0.115193 -0.059153  0.073709  0.071881   \n",
       "Pclass_1          0.026495  0.285904 -0.107371  0.362587 -0.034256 -0.013033   \n",
       "Pclass_2          0.022714  0.093349 -0.028862 -0.014193 -0.052419 -0.010057   \n",
       "Pclass_3         -0.041544 -0.322308  0.116562 -0.302093  0.072610  0.019521   \n",
       "Master            0.002254  0.085221  0.164375 -0.363923  0.329171  0.253482   \n",
       "Miss             -0.050027  0.332795 -0.672819 -0.254146  0.077564  0.066473   \n",
       "Mr                0.014116 -0.549199  0.870678  0.165476 -0.243104 -0.304780   \n",
       "Mrs               0.033299  0.344935 -0.571176  0.198091  0.061643  0.213491   \n",
       "Officer           0.002231 -0.031316  0.087288  0.162818 -0.013813 -0.032631   \n",
       "Royalty           0.004400  0.033391 -0.020408  0.059466 -0.010787 -0.030197   \n",
       "Cabin_A          -0.002831  0.022287  0.047561  0.125177 -0.039808 -0.030707   \n",
       "Cabin_B           0.015895  0.175095 -0.094453  0.113458 -0.011569  0.073051   \n",
       "Cabin_C           0.006092  0.114652 -0.077473  0.167993  0.048616  0.009601   \n",
       "Cabin_D           0.000549  0.150716 -0.057396  0.132886 -0.015727 -0.027385   \n",
       "Cabin_E          -0.008136  0.145321 -0.040340  0.106600 -0.027180  0.001084   \n",
       "Cabin_F           0.000306  0.057935 -0.006655 -0.072644 -0.008619  0.020481   \n",
       "Cabin_G          -0.045949  0.016040 -0.083285 -0.085977  0.006015  0.058325   \n",
       "Cabin_T          -0.023049 -0.026456  0.020558  0.032461 -0.013247 -0.012304   \n",
       "Cabin_U           0.000208 -0.316912  0.137396 -0.271918  0.009064 -0.036806   \n",
       "FamilySize       -0.031437  0.016639 -0.188583 -0.196996  0.861952  0.792296   \n",
       "Family_Single     0.028546 -0.203367  0.284537  0.116675 -0.591077 -0.549022   \n",
       "Family_Small      0.002975  0.279855 -0.255196 -0.038189  0.253590  0.248532   \n",
       "Family_Large     -0.063415 -0.125147 -0.077748 -0.161210  0.699681  0.624627   \n",
       "\n",
       "                   Fare  Embarked_C  Embarked_Q  Embarked_S  ...   Cabin_D  \\\n",
       "PassengerId    0.031416    0.048101    0.011585   -0.049836  ...  0.000549   \n",
       "Survived       0.257307    0.168240    0.003650   -0.149683  ...  0.150716   \n",
       "Sex           -0.185484   -0.066564   -0.088651    0.115193  ... -0.057396   \n",
       "Age            0.171521    0.076179   -0.012718   -0.059153  ...  0.132886   \n",
       "SibSp          0.160224   -0.048396   -0.048678    0.073709  ... -0.015727   \n",
       "Parch          0.221522   -0.008635   -0.100943    0.071881  ... -0.027385   \n",
       "Fare           1.000000    0.286241   -0.130054   -0.169894  ...  0.072737   \n",
       "Embarked_C     0.286241    1.000000   -0.164166   -0.778262  ...  0.107782   \n",
       "Embarked_Q    -0.130054   -0.164166    1.000000   -0.491656  ... -0.061459   \n",
       "Embarked_S    -0.169894   -0.778262   -0.491656    1.000000  ... -0.056023   \n",
       "Pclass_1       0.599956    0.325722   -0.166101   -0.181800  ...  0.275698   \n",
       "Pclass_2      -0.121372   -0.134675   -0.121973    0.196532  ... -0.037929   \n",
       "Pclass_3      -0.419616   -0.171430    0.243706   -0.003805  ... -0.207455   \n",
       "Master         0.011596   -0.014172   -0.009091    0.018297  ... -0.042192   \n",
       "Miss           0.092051   -0.014351    0.198804   -0.113886  ... -0.012516   \n",
       "Mr            -0.192192   -0.065538   -0.080224    0.108924  ... -0.030261   \n",
       "Mrs            0.139235    0.098379   -0.100374   -0.022950  ...  0.080393   \n",
       "Officer        0.028696    0.003678   -0.003212   -0.001202  ...  0.006055   \n",
       "Royalty        0.026214    0.077213   -0.021853   -0.054250  ... -0.012950   \n",
       "Cabin_A        0.020094    0.094914   -0.042105   -0.056984  ... -0.024952   \n",
       "Cabin_B        0.393743    0.161595   -0.073613   -0.095790  ... -0.043624   \n",
       "Cabin_C        0.401370    0.158043   -0.059151   -0.101861  ... -0.053083   \n",
       "Cabin_D        0.072737    0.107782   -0.061459   -0.056023  ...  1.000000   \n",
       "Cabin_E        0.073949    0.027566   -0.042877    0.002960  ... -0.034317   \n",
       "Cabin_F       -0.037567   -0.020010   -0.020282    0.030575  ... -0.024369   \n",
       "Cabin_G       -0.022857   -0.031566   -0.019941    0.040560  ... -0.011817   \n",
       "Cabin_T        0.001179   -0.014095   -0.008904    0.018111  ... -0.005277   \n",
       "Cabin_U       -0.507197   -0.258257    0.142369    0.137351  ... -0.353822   \n",
       "FamilySize     0.226465   -0.036553   -0.087190    0.087771  ... -0.025313   \n",
       "Family_Single -0.274826   -0.107874    0.127214    0.014246  ... -0.074310   \n",
       "Family_Small   0.197281    0.159594   -0.122491   -0.062909  ...  0.102432   \n",
       "Family_Large   0.170853   -0.092825   -0.018423    0.093671  ... -0.049336   \n",
       "\n",
       "                Cabin_E   Cabin_F   Cabin_G   Cabin_T   Cabin_U  FamilySize  \\\n",
       "PassengerId   -0.008136  0.000306 -0.045949 -0.023049  0.000208   -0.031437   \n",
       "Survived       0.145321  0.057935  0.016040 -0.026456 -0.316912    0.016639   \n",
       "Sex           -0.040340 -0.006655 -0.083285  0.020558  0.137396   -0.188583   \n",
       "Age            0.106600 -0.072644 -0.085977  0.032461 -0.271918   -0.196996   \n",
       "SibSp         -0.027180 -0.008619  0.006015 -0.013247  0.009064    0.861952   \n",
       "Parch          0.001084  0.020481  0.058325 -0.012304 -0.036806    0.792296   \n",
       "Fare           0.073949 -0.037567 -0.022857  0.001179 -0.507197    0.226465   \n",
       "Embarked_C     0.027566 -0.020010 -0.031566 -0.014095 -0.258257   -0.036553   \n",
       "Embarked_Q    -0.042877 -0.020282 -0.019941 -0.008904  0.142369   -0.087190   \n",
       "Embarked_S     0.002960  0.030575  0.040560  0.018111  0.137351    0.087771   \n",
       "Pclass_1       0.242963 -0.073083 -0.035441  0.048310 -0.776987   -0.029656   \n",
       "Pclass_2      -0.050210  0.127371 -0.032081 -0.014325  0.176485   -0.039976   \n",
       "Pclass_3      -0.169063 -0.041178  0.056964 -0.030057  0.527614    0.058430   \n",
       "Master         0.001860  0.058311 -0.013690 -0.006113  0.041178    0.355061   \n",
       "Miss           0.008700 -0.003088  0.061881 -0.013832 -0.004364    0.087350   \n",
       "Mr            -0.032953 -0.026403 -0.072514  0.023611  0.131807   -0.326487   \n",
       "Mrs            0.045538  0.013376  0.042547 -0.011742 -0.162253    0.157233   \n",
       "Officer       -0.024048 -0.017076 -0.008281 -0.003698 -0.067030   -0.026921   \n",
       "Royalty       -0.012202 -0.008665 -0.004202 -0.001876 -0.071672   -0.023600   \n",
       "Cabin_A       -0.023510 -0.016695 -0.008096 -0.003615 -0.242399   -0.042967   \n",
       "Cabin_B       -0.041103 -0.029188 -0.014154 -0.006320 -0.423794    0.032318   \n",
       "Cabin_C       -0.050016 -0.035516 -0.017224 -0.007691 -0.515684    0.037226   \n",
       "Cabin_D       -0.034317 -0.024369 -0.011817 -0.005277 -0.353822   -0.025313   \n",
       "Cabin_E        1.000000 -0.022961 -0.011135 -0.004972 -0.333381   -0.017285   \n",
       "Cabin_F       -0.022961  1.000000 -0.007907 -0.003531 -0.236733    0.005525   \n",
       "Cabin_G       -0.011135 -0.007907  1.000000 -0.001712 -0.114803    0.035835   \n",
       "Cabin_T       -0.004972 -0.003531 -0.001712  1.000000 -0.051263   -0.015438   \n",
       "Cabin_U       -0.333381 -0.236733 -0.114803 -0.051263  1.000000   -0.014155   \n",
       "FamilySize    -0.017285  0.005525  0.035835 -0.015438 -0.014155    1.000000   \n",
       "Family_Single -0.042535  0.004055 -0.076397  0.022411  0.175812   -0.688864   \n",
       "Family_Small   0.068007  0.012756  0.087471 -0.019574 -0.211367    0.302640   \n",
       "Family_Large  -0.046485 -0.033009 -0.016008 -0.007148  0.056438    0.801623   \n",
       "\n",
       "               Family_Single  Family_Small  Family_Large  \n",
       "PassengerId         0.028546      0.002975     -0.063415  \n",
       "Survived           -0.203367      0.279855     -0.125147  \n",
       "Sex                 0.284537     -0.255196     -0.077748  \n",
       "Age                 0.116675     -0.038189     -0.161210  \n",
       "SibSp              -0.591077      0.253590      0.699681  \n",
       "Parch              -0.549022      0.248532      0.624627  \n",
       "Fare               -0.274826      0.197281      0.170853  \n",
       "Embarked_C         -0.107874      0.159594     -0.092825  \n",
       "Embarked_Q          0.127214     -0.122491     -0.018423  \n",
       "Embarked_S          0.014246     -0.062909      0.093671  \n",
       "Pclass_1           -0.126551      0.165965     -0.067523  \n",
       "Pclass_2           -0.035075      0.097270     -0.118495  \n",
       "Pclass_3            0.138250     -0.223338      0.155560  \n",
       "Master             -0.265355      0.120166      0.301809  \n",
       "Miss               -0.023890     -0.018085      0.083422  \n",
       "Mr                  0.386262     -0.300872     -0.194207  \n",
       "Mrs                -0.354649      0.361247      0.012893  \n",
       "Officer             0.013303      0.003966     -0.034572  \n",
       "Royalty             0.008761     -0.000073     -0.017542  \n",
       "Cabin_A             0.045227     -0.029546     -0.033799  \n",
       "Cabin_B            -0.087912      0.084268      0.013470  \n",
       "Cabin_C            -0.137498      0.141925      0.001362  \n",
       "Cabin_D            -0.074310      0.102432     -0.049336  \n",
       "Cabin_E            -0.042535      0.068007     -0.046485  \n",
       "Cabin_F             0.004055      0.012756     -0.033009  \n",
       "Cabin_G            -0.076397      0.087471     -0.016008  \n",
       "Cabin_T             0.022411     -0.019574     -0.007148  \n",
       "Cabin_U             0.175812     -0.211367      0.056438  \n",
       "FamilySize         -0.688864      0.302640      0.801623  \n",
       "Family_Single       1.000000     -0.873398     -0.318944  \n",
       "Family_Small       -0.873398      1.000000     -0.183007  \n",
       "Family_Large       -0.318944     -0.183007      1.000000  \n",
       "\n",
       "[32 rows x 32 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "source": [
    "'''\r\n",
    "查看各个特征与生存情况（Survived）的相关系数，\r\n",
    "ascending=False表示按降序排列\r\n",
    "'''\r\n",
    "corrDF['Survived'].sort_values(ascending = False)\r\n",
    "\r\n",
    "# 大于0的为正线性相关。小于0的为负线性相关。等于0表示不相关"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Survived         1.000000\n",
       "Mrs              0.344935\n",
       "Miss             0.332795\n",
       "Pclass_1         0.285904\n",
       "Family_Small     0.279855\n",
       "Fare             0.257307\n",
       "Cabin_B          0.175095\n",
       "Embarked_C       0.168240\n",
       "Cabin_D          0.150716\n",
       "Cabin_E          0.145321\n",
       "Cabin_C          0.114652\n",
       "Pclass_2         0.093349\n",
       "Master           0.085221\n",
       "Parch            0.081629\n",
       "Cabin_F          0.057935\n",
       "Royalty          0.033391\n",
       "Cabin_A          0.022287\n",
       "FamilySize       0.016639\n",
       "Cabin_G          0.016040\n",
       "Embarked_Q       0.003650\n",
       "PassengerId     -0.005007\n",
       "Cabin_T         -0.026456\n",
       "Officer         -0.031316\n",
       "SibSp           -0.035322\n",
       "Age             -0.070323\n",
       "Family_Large    -0.125147\n",
       "Embarked_S      -0.149683\n",
       "Family_Single   -0.203367\n",
       "Cabin_U         -0.316912\n",
       "Pclass_3        -0.322308\n",
       "Sex             -0.543351\n",
       "Mr              -0.549199\n",
       "Name: Survived, dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "根据各个特征与生存情况（Survived）的相关系数大小，我们选择了这几个特征作为模型的输入：\r\n",
    "\r\n",
    "头衔（前面所在的数据集titleDF）、客舱等级（PclassDF）、家庭大小（familyDF）、船票价格（Fare）、客舱号（CabinDF）、登船港口（EmbarkedDF）、性别（Sex）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "source": [
    "# 特征选择\r\n",
    "full_X = pd.concat([titleDF, #头衔\r\n",
    "                    PclassDF, #客舱等级\r\n",
    "                    familyDF, #家庭大小\r\n",
    "                    full['Fare'], #船票价格\r\n",
    "                    CabinDF, #客舱号\r\n",
    "                    EmbarkedDF, #登船港口\r\n",
    "                    full['Sex'] #性别\r\n",
    "                    ], axis=1)\r\n",
    "\r\n",
    "full_X.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Master</th>\n",
       "      <th>Miss</th>\n",
       "      <th>Mr</th>\n",
       "      <th>Mrs</th>\n",
       "      <th>Officer</th>\n",
       "      <th>Royalty</th>\n",
       "      <th>Pclass_1</th>\n",
       "      <th>Pclass_2</th>\n",
       "      <th>Pclass_3</th>\n",
       "      <th>FamilySize</th>\n",
       "      <th>...</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_U</th>\n",
       "      <th>Embarked_C</th>\n",
       "      <th>Embarked_Q</th>\n",
       "      <th>Embarked_S</th>\n",
       "      <th>Sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Master  Miss  Mr  Mrs  Officer  Royalty  Pclass_1  Pclass_2  Pclass_3  \\\n",
       "0       0     0   1    0        0        0         0         0         1   \n",
       "1       0     0   0    1        0        0         1         0         0   \n",
       "2       0     1   0    0        0        0         0         0         1   \n",
       "3       0     0   0    1        0        0         1         0         0   \n",
       "4       0     0   1    0        0        0         0         0         1   \n",
       "\n",
       "   FamilySize  ...  Cabin_D  Cabin_E  Cabin_F  Cabin_G  Cabin_T  Cabin_U  \\\n",
       "0           2  ...        0        0        0        0        0        1   \n",
       "1           2  ...        0        0        0        0        0        0   \n",
       "2           1  ...        0        0        0        0        0        1   \n",
       "3           2  ...        0        0        0        0        0        0   \n",
       "4           1  ...        0        0        0        0        0        1   \n",
       "\n",
       "   Embarked_C  Embarked_Q  Embarked_S  Sex  \n",
       "0           0           0           1    1  \n",
       "1           1           0           0    0  \n",
       "2           0           0           1    0  \n",
       "3           0           0           1    0  \n",
       "4           0           0           1    1  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 44
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4.构建模型"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "用训练数据和某个机器学习算法得到机器学习模型，用测试数据评估模型"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.1 建立训练数据集和测试数据集"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "source": [
    "'''\r\n",
    "1）泰坦尼克号测试数据集,因为是最后要提交给Kaggle的，里面没有生存情况的值，所以不能用于评估模型。\r\n",
    "Kaggle泰坦尼克号项目给的测试数据，叫做预测数据集（记为pred,也就是预测英文单词predict的缩写）。\r\n",
    "也就是使用机器学习模型来对其生存情况的预测。\r\n",
    "2）使用Kaggle泰坦尼克号项目给的训练数据集，做为原始数据集（记为source），\r\n",
    "从这个原始数据集中拆分出训练数据集（记为train：用于模型训练）和测试数据集（记为test：用于模型评估）。\r\n",
    "'''\r\n",
    "# 原始数据集有891行\r\n",
    "sourceRow = 891\r\n",
    "\r\n",
    "'''\r\n",
    "sourceRow是我们在最开始合并数据前知道的，原始数据集有总共有891条数据\r\n",
    "从特征集合full_X中提取原始数据集提取前891行数据时，我们要减去1，因为行号是从0开始的。\r\n",
    "'''\r\n",
    "# 原始数据集：特征\r\n",
    "source_X = full_X.loc[0:sourceRow-1, :]\r\n",
    "# 原始数据集：标签\r\n",
    "source_y = full.loc[0:sourceRow-1, 'Survived']\r\n",
    "\r\n",
    "# 预测数据集：特征\r\n",
    "pred_X = full_X.loc[sourceRow:, :]\r\n",
    "'''\r\n",
    "上面代码解释：\r\n",
    "891行前面的数据是测试数据集，891行之后的数据是预测数据集。[sourceRow:,:]就是从891行开始到最后一行作为预测数据集\r\n",
    "'''"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'\\n上面代码解释：\\n891行前面的数据是测试数据集，891行之后的数据是预测数据集。[sourceRow:,:]就是从891行开始到最后一行作为预测数据集\\n'"
      ]
     },
     "metadata": {},
     "execution_count": 50
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "source": [
    "'''\r\n",
    "确保这里原始数据集取的是前891行的数据，不然后面模型会有错误\r\n",
    "'''\r\n",
    "# 原始数据集有多少行\r\n",
    "print('原始数据集有多少行：', source_X.shape[0])\r\n",
    "\r\n",
    "# 预测数据集有多少行\r\n",
    "print('预测数据集有多少行：', pred_X.shape[0])"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "原始数据集有多少行： 891\n",
      "预测数据集有多少行： 418\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "source": [
    "'''\r\n",
    "从原始数据集（source）中拆分出训练数据集（用于模型训练train），测试数据集（用于模型评估test）\r\n",
    "train_test_split是交叉验证中常用的函数，功能是从样本中随机的按比例选取train data和test data\r\n",
    "train_data：所要划分的样本特征集\r\n",
    "train_target：所要划分的样本结果\r\n",
    "test_size：样本占比，如果是整数的话就是样本的数量\r\n",
    "'''\r\n",
    "from sklearn.model_selection import train_test_split\r\n",
    "\r\n",
    "train_X, test_X, train_y, test_y = train_test_split(source_X, source_y, train_size=0.8)\r\n",
    "# 80%为训练数据。20%为测试数据\r\n",
    "\r\n",
    "# 输出数据集大小\r\n",
    "print('原始数据集特征：', source_X.shape,\r\n",
    "        '\\n训练数据集特征：', train_X.shape,\r\n",
    "        '\\n测试数据集特征：', test_X.shape)\r\n",
    "\r\n",
    "print('原始数据集标签：', source_y.shape,\r\n",
    "        '\\n训练数据集标签：', train_y.shape,\r\n",
    "        '\\n测试数据集标签：', test_y.shape)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "原始数据集特征： (891, 27) \n",
      "训练数据集特征： (712, 27) \n",
      "测试数据集特征： (179, 27)\n",
      "原始数据集标签： (891,) \n",
      "训练数据集标签： (712,) \n",
      "测试数据集标签： (179,)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "source": [
    "# 原始数据查看\r\n",
    "source_y.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    0.0\n",
       "1    1.0\n",
       "2    1.0\n",
       "3    1.0\n",
       "4    0.0\n",
       "Name: Survived, dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 54
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2 选择机器学习算法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "选择**逻辑回归算法**作为机器学习算法，用于模型的训练"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.1 导入算法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "source": [
    "from sklearn.linear_model import LogisticRegression\r\n",
    "# 逻辑回归算法"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.2 创建模型"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "source": [
    "model = LogisticRegression()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3 训练模型"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "source": [
    "'''\r\n",
    "train_X 训练数据集特征（头衔、性别等）\r\n",
    "train_y 训练数据集标签（Survived）\r\n",
    "输出：分类结果（存活1，死亡0）\r\n",
    "'''\r\n",
    "model.fit(train_X, train_y)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "LogisticRegression()"
      ]
     },
     "metadata": {},
     "execution_count": 57
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5.模型评估"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "source": [
    "'''\r\n",
    "test_X 测试数据集特征（头衔、性别等）\r\n",
    "test_y 测试数据集标签（Survived）\r\n",
    "输出：分类问题，score得到的是模型的正确率\r\n",
    "'''\r\n",
    "model.score(test_X, test_y)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0.8100558659217877"
      ]
     },
     "metadata": {},
     "execution_count": 58
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 6.方案实施（Deployment）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 6.1 得到预测结果上传到Kaggle"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用预测数据集来预测结果，并保存到csv文件中，然后上传到Kaggle中，就可以看到排名。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "source": [
    "# 使用机器学习模型，对预测数据集中的生存情况进行预测\r\n",
    "pred_Y = model.predict(pred_X)\r\n",
    "\r\n",
    "'''\r\n",
    "生成的预测值是浮点数（0.0, 1.0）\r\n",
    "但是Kaggle要求提交的结果是整型（0, 1）\r\n",
    "所以要对数据类型进行转换\r\n",
    "'''\r\n",
    "pred_Y = pred_Y.astype(int)\r\n",
    "# 乘客id\r\n",
    "passenger_id = full.loc[sourceRow:, 'PassengerId']\r\n",
    "# 数据框：乘客id，预测生存情况的值\r\n",
    "predDF = pd.DataFrame({'PassengerId': passenger_id,\r\n",
    "                        'Survived': pred_Y})\r\n",
    "print(predDF.shape)\r\n",
    "print(predDF.head())\r\n",
    "predDF.to_csv('titanic_pred.csv', index=False)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "(418, 2)\n",
      "     PassengerId  Survived\n",
      "891          892         0\n",
      "892          893         1\n",
      "893          894         0\n",
      "894          895         0\n",
      "895          896         1\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 6.2 报告撰写"
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.11",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.11 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "e889ae53dd75a4248c126b71a27b09b582022e61106032531b7e5844103528be"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}